package com.example.springboot.service;

import java.util.List;

import com.example.springboot.mapper.UserMapper;
import com.example.springboot.pojo.Role;
import com.example.springboot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class UserServiceImplement implements UserService{

	@Resource
	UserMapper userMapper;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user =userMapper.queryUserByUsername(username);
		if(user==null) {
			throw new UsernameNotFoundException("用户不存在");
		}
		List<Role> roles=userMapper.queryRolesByUid(user.getUid());
		user.setRoles(roles);
		return user;
	}

	@Override
	public List<User> query(String username, Integer enabled) {
		return userMapper.query(username,enabled);
	}

	@Override
	public User queryById(Integer id) {
		return userMapper.queryById(id);
	}

	@Override
	public int del(Integer id) {
		int result=userMapper.del(id);
		int result2=userMapper.delUserRole(id);
		return result*result2;
	}

}
